package com.xingdata.jjxc.m.login.avt;

import java.util.HashMap;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.xingdata.jjxc.App;
import com.xingdata.jjxc.R;
import com.xingdata.jjxc.activity.LoginActivity;
import com.xingdata.jjxc.base.Base1Activity;
import com.xingdata.jjxc.enty.RespEntity;
import com.xingdata.jjxc.enty.UserEntity;
import com.xingdata.jjxc.http.HttpResponseListener;
import com.xingdata.jjxc.utils.ActivityTools;
import com.xingdata.jjxc.utils.MessageCode;
import com.xingdata.jjxc.utils.SystemInfo;
import com.xingdata.jjxc.views.AnimationView;
import com.xingdata.jjxc.views.CircleImageView;

public class Register2 extends Base1Activity implements
		OnClickListener {

	private EditText registertwo_pwd1, registertwo_pwd2;
	private Button registertwo_btn;

	private String phone;
	private String checknum;
	private ImageView imageView_pic;
	private Handler mHandler;
	private String type;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		ExitApplication.getInstance().addActivity(this);
		Bundle bundle = getIntent().getExtras();
		phone = bundle.getString("phone");
		checknum = bundle.getString("checknum");
		type= bundle.getString("type");
//		ExitApplication.getInstance().addActivity(this);
		
		mHandler = new Handler() {
			public void handleMessage(android.os.Message msg) {
				switch (msg.what) {
				case MessageCode.MessageCode_ShowToast:
					showToast(msg.getData().getString("result"));
					break;

				default:
					break;
				}
			};
		};
	}

	@Override
	protected int getContentViewId() {
		return R.layout.activity_register2;
	}

	@Override
	protected String getTitleText() {
		return "立即注册";
	}

	@Override
	protected void initViews() {
		registertwo_pwd1 = (EditText) findViewById(R.id.registertwo_pwd1_et);
		registertwo_pwd2 = (EditText) findViewById(R.id.registertwo_pwd2_et);
		registertwo_btn = (Button) findViewById(R.id.registertwo_btn);
		registertwo_btn.setOnClickListener(this);
		imageView_pic = (ImageView) findViewById(R.id.imageView_pic);
		new AnimationView(this, imageView_pic).init();
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.registertwo_btn:
			String pswStr = registertwo_pwd1.getText().toString().trim();
			String pswRewStr = registertwo_pwd2.getText().toString().trim();

			if (pswStr.length() < 6) {
				showToast(R.string.error_pwd_length);
			} else if (!pswStr.equals(pswRewStr)) {
				showToast(R.string.error_pwd_not_same);
				registertwo_pwd2.setText("");
			} else {
				doPost_register(phone, pswStr, checknum);
			}
			break;

		default:
			break;
		}
	}

	private void doPost_register(final String phone, final String pwd,
			final String checknum) {
		String url="";
		if (type.equals("0")) {
			url=App.ZZD_REQUEST_REGISTER;
		}else {
			url=App.ZZD_REQUEST_REGISTER;
		}
		if (isNetworkConnected() == false) {
			showToast(R.string.network);
			return;
		}

		paramsMap = new HashMap<String, String>();
		paramsMap.put("mobile", phone);
		paramsMap.put("password", pwd);
		paramsMap.put("checknum", checknum);
		mHttpUtil.Post(App.ZZD_REQUEST_REGISTER, paramsMap,
				new HttpResponseListener() {
					
					@Override
					public void onSuccess(RespEntity respEntity) {
						// TODO Auto-generated method stub
						Bundle bundle = new Bundle();
						bundle.putString("result",
								getResources().getString(R.string.register_ok));
						Message message = new Message();
						message.setData(bundle);
						message.what = MessageCode.MessageCode_ShowToast;
						mHandler.sendMessage(message);
						UserEntity entity = SystemInfo.getUserInfo();
						// entity.setPw(pwd);
						// 加密之后 存储
//						entity.setPw(Code.CodeMD5(
//								Code.CodeMD5(pwd).toUpperCase()).toUpperCase());
//						SP.saveUserInfo(Register2.this, entity);
						Intent intent = new Intent(Register2.this,
								LoginActivity.class);
						bundle = new Bundle();
						bundle.putString("phone", phone);
						intent.putExtras(bundle);
						ActivityTools.startActivity(Register2.this, intent);
						finish();
					}
					
					@Override
					public void onFailure(String result) {
						showToast(result);
					}

					@Override
					public void onLoading(int vurrent) {
						// TODO Auto-generated method stub
						
					}
				});
	}

}
